<template>
  <div>

    <!--添加用户信息-->
    <div style="margin-left: 20px">
      <el-button type="primary" icon="el-icon-notebook-2" @click="allLogs()">
        全部日志
      </el-button>
      <el-button type="primary" icon="el-icon-delete-solid" @click="clearLogs()">
        清空日志
      </el-button>
      <div style="position: relative;margin-bottom: 20px;float: right">
        <div class="block">
          <el-date-picker
              v-model="date.dateTime"
              type="date"
              placeholder="请选择日期"
              value-format="yyyy-MM-dd"
          >
          </el-date-picker>
          <el-button slot="append" type="primary" icon="el-icon-search" @click="dateSearch()" style="color: #f6f7f8">
            搜索
          </el-button>
        </div>

      </div>
    </div>

      <el-table
                :data="LogArr"
                style="width: 100%;margin:0 20px 20px 20px" border>
        <el-table-column type="index" label="" width="50" align="center"></el-table-column>
        <el-table-column prop="id" label="编号" width="100" align="center"></el-table-column>
        <el-table-column prop="username" label="用户名" align="center"></el-table-column>
        <el-table-column prop="created" label="操作时间" align="center"></el-table-column>
        <el-table-column prop="method" label="操作" align="center"></el-table-column>
        <el-table-column prop="time" label="持续时间" align="center"></el-table-column>
        <el-table-column prop="error" label="出错原因" align="center"></el-table-column>
      </el-table>
      <!--用户列表结束-->
      <!--分页处理-->
      <div style="text-align: left" v-if="show===1">
        <el-row>
          <el-col :span="12">
            <el-pagination
                style="margin: 15px 0px"
                background
                layout="prev, pager, next, jumper, total, sizes"
                :page-size="size"
                :current-page="now"
                :total="total"
                :page-sizes="[2, 4, 6, 8]"
                @current-change="findPage"
                @size-change="findSize">
            </el-pagination>
          </el-col>
        </el-row>
      </div>
      <!--分页处理结束-->
  </div>
</template>

<script>

export default {
  data() {
    return {

      /*用户数组*/
      LogArr: [],
      /*表分页*/
      total: '',
      now: 1,
      size: 6,
      input2: "",
      /*表分页结束*/
      date: { dateTime: '',page:'',pageSize:''},
      show:1,

    };
  },
  methods: {
    clearLogs(){
      let url = 'http://localhost:8080/logs/deleteAll';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response)=> {
       if (response.data.state==20000){
         this.$message.success("日志已全部清除")
         this.showAllLogsInfo();
       }else {
         this.$message.success("日志删除失败")
       }
      })
    },
    allLogs(){
      this.showAllLogsInfo();
    },
    dateSearch(currentPage, pageSize){
      currentPage = currentPage ? currentPage : this.now;
      pageSize = pageSize ? pageSize : this.size;
      this.date.page=currentPage;
      this.date.pageSize = pageSize;
      if (this.date.dateTime === '') {
        this.$message.error("选择的时间不能为空");
        return;
      }
      let url = 'http://localhost:8080/logs/search-date';
      let formData = this.qs.stringify(this.date);
      console.log("formData"+formData)
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url,formData).then((response)=> {
        this.LogArr = response.data.data.list;
        this.total=response.data.data.totalPage;
        this.date.dateTime = '';
      })
    },

    /*表分页方法*/
    findPage(now_page) {
      //用来处理当前页的方法,page为当前页
      this.now = now_page;
      this.showAllLogsInfo(now_page, this.size);
    },
    findSize(now_size) {
      //用来处理每页条数的方法,size为当前页条数
      this.size = now_size;
      this.showAllLogsInfo(this.now, now_size);
    },
    // 分页展示所有的用户信息
    showAllLogsInfo(currentPage, pageSize) {
      currentPage = currentPage ? currentPage : this.now;
      pageSize = pageSize ? pageSize : this.size;
      let url = "http://localhost:8080/logs/list?page=" + currentPage + "&pageSize=" + pageSize;
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response)=>{
        this.LogArr = response.data.data.list;
        this.total=response.data.data.totalPage;
      })
    },
    allUser() {
      let url = "http://localhost:8080/users/rebuild-cache"
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((res)=>{})
      location.href="/sys-admin/temp/user/list"

    },
    },
  mounted() {
    /*分页查找用户信息*/
    this.showAllLogsInfo();
  }
}
</script>